﻿using CatUtils.Utils.Currency;
using System;
using System.Collections.Generic;
using System.Net;
using static CatUtils.Utils.Currency.PrintLog;

namespace CatUtilsDll.Utils.Currency.WebUtils
{/// <summary>
 /// 网络请求帮助
 /// </summary>
    public partial class WebHelper
    {
        /// <summary>
        /// 获取301OR302转移状态码，侦测资源是否发生了转移 转移返回true,可定义响应出错关键词列表，如果在错误信息中查到列表关键词，则返回true
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <returns></returns>
        public bool GetStatusMove(string RequestUrl)
        {
            return GetStatusMove(RequestUrl, new List<string>() { "System.Net.FileWebRequest", "404", "未能确定", "无效的" });
        }

        /// <summary>
        /// 获取301OR302转移状态码，侦测资源是否发生了转移 转移返回true,可定义响应出错关键词列表，如果在错误信息中查到列表关键词，则返回true
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="AllowAutoRedirect">是否跟随重定向</param>
        /// <returns></returns>
        public bool GetStatusMove(string RequestUrl, bool AllowAutoRedirect)
        {
            return GetStatusMove(RequestUrl, new List<string>() { "System.Net.FileWebRequest", "404", "未能确定", "无效的" }, AllowAutoRedirect);
        }

        /// <summary>
        /// 获取301OR302转移状态码，侦测资源是否发生了转移 转移返回true,可定义响应出错关键词列表，如果在错误信息中查到列表关键词，则返回true
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="MoveErrorTag">如果出现了错误，错误中出现哪些关键词返回True</param>
        /// <returns></returns>
        public bool GetStatusMove(string RequestUrl, List<string> MoveErrorTag)
        {
            return GetStatusMove(CreateHttpRequest(RequestUrl, true), RequestUrl, MoveErrorTag);
        }

        /// <summary>
        /// 获取301OR302转移状态码，侦测资源是否发生了转移 转移返回true,可定义响应出错关键词列表，如果在错误信息中查到列表关键词，则返回true
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="MoveErrorTag"></param>
        /// <param name="AllowAutoRedirect">是否跟随重定向</param>
        /// <returns></returns>
        public bool GetStatusMove(string RequestUrl, List<string> MoveErrorTag, bool AllowAutoRedirect)
        {
            return GetStatusMove(CreateHttpRequest(RequestUrl, AllowAutoRedirect), RequestUrl, MoveErrorTag);
        }

        /// <summary>
        /// 获取301OR302转移状态码，侦测资源是否发生了转移 转移返回true,可定义响应出错关键词列表，如果在错误信息中查到列表关键词，则返回true
        /// </summary>
        /// <param name="webRequest"></param>
        /// <param name="RequestUrl"></param>
        /// <param name="MoveErrorTag">如果出现了错误，错误中出现哪些关键词返回True</param>
        /// <returns></returns>
        public bool GetStatusMove(HttpWebRequest webRequest, string RequestUrl, List<string> MoveErrorTag)
        {
            if (webRequest == null)
            {
                Console.WriteLine("初始化Httprequest失败，请检查输入的URL");
                return true;
            }

            try
            {
                using (HttpWebResponse httpRes = (HttpWebResponse)webRequest.GetResponse())
                {
                    if (httpRes.StatusCode == HttpStatusCode.Moved || httpRes.StatusCode == HttpStatusCode.Found)
                    {
                        // Code for moved resources goes here.
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                string ErrorMessage = ex.Message;

                if (ListHelper.CompareRepetition(ErrorMessage, MoveErrorTag))
                    return true;

                PrintWebLog(RequestUrl, ex);
            }
            return false;
        }

        /// <summary>
        ///  获取http响应状态码的类，需要连接
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="AllowAutoRedirect">是否跟随重定向</param>
        /// <returns></returns>
        public static HttpStatusCode GetStatusCode(string RequestUrl, bool AllowAutoRedirect)
        {
            return GetStatusCode(CreateHttpRequest(RequestUrl, AllowAutoRedirect), RequestUrl);
        }

        /// <summary>
        ///  获取http响应状态码的类，需要连接
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <returns></returns>
        public static HttpStatusCode GetStatusCode(string RequestUrl)
        {
            return GetStatusCode(CreateHttpRequest(RequestUrl, true), RequestUrl);
        }

        /// <summary>
        /// 获取URL状态码
        /// </summary>
        /// <param name="httpReq"></param>
        /// <param name="RequestUrl"></param>
        /// <returns></returns>
        public static HttpStatusCode GetStatusCode(HttpWebRequest httpReq, string RequestUrl)
        {
            if (httpReq == null)
            {
                E($"初始化Httprequest失败，请检查输入的URL：{RequestUrl}");
                return HttpStatusCode.HttpVersionNotSupported;
            }
            try
            {
                using (HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse())
                {
                    return httpRes.StatusCode;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //404 403之类的错误会在这里出现
            }
            return HttpStatusCode.GatewayTimeout;
        }
    }
}